﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Data.OleDb;
using System.Data.SqlTypes;

namespace DATN.mds.TradingReport
{
    public partial class TradingReport : System.Web.UI.Page
    {
        private string cnnString;
        SqlConnection cnn;
        SqlCommand cmd;

        protected void Page_Load(object sender, EventArgs e)
        {
            Chart1.Visible = false;
            Chart5.Visible = false;

            if (!IsPostBack)
            {
                // Trading cho mot Opportunity:
                Drr();
            }
        }

        //Load Chart 
        protected void LoadChart()
        {
            Chart1.Visible = true;
            Chart5.Visible = true;

            double a = Convert.ToDouble(lb_Fundx.Text);
            double b = Convert.ToDouble(lb_Profitx.Text);
            double c = Convert.ToDouble(lb_AcEx.Text);
            double d = Convert.ToDouble(lb_AcRe.Text);
            double[] yVal = { a, b, c, d};
            string[] xName = { "Vốn", "Lợi Nhuận dự tính", "Chi Phí Thực Tế", "Lợi Nhuận" };
            Chart1.Series[0].Points.DataBindXY(xName, yVal);
            Chart1.Series[0].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Column;

            Chart5.Series[0].Points.DataBindXY(xName, yVal);
            Chart5.Series[0].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Radar;
        }
      
        //----------------------------------------------//
        //Lấy được SUM của Real Expense và Real Revenue.
        protected void bt_Value_Click(object sender, EventArgs e)
        {
            try
            {
                cnnString = ConfigurationManager.ConnectionStrings["DATNconnection"].ToString();
                cnn = new SqlConnection(cnnString);
                cnn.Open();

                string SearchQuery = "SELECT Sum(ActionPlan.ActualExpense), "
                                + " Sum(ActionPlan.ActualRevenue) FROM ActionPlan "
                                + " WHERE @DateStart < Time "
                                + " AND  Time < @DateClose "
                                + " AND Opp_Id = @Opp_Id ";

                cmd = new SqlCommand(SearchQuery, cnn);

                cmd.Parameters.Add("@DateStart", SqlDbType.DateTime);
                cmd.Parameters["@DateStart"].Value = tbx_DateStart.Text;

                cmd.Parameters.Add("@DateClose", SqlDbType.DateTime);
                cmd.Parameters["@DateClose"].Value = tbx_DateClose.Text;

                cmd.Parameters.Add("@Opp_Id", SqlDbType.Int);
                cmd.Parameters["@Opp_Id"].Value = Convert.ToInt16(ddr_Opp.SelectedValue);

                SqlDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    lb_AcEx.Text = reader[0].ToString();
                    lb_AcRe.Text = reader[1].ToString();
                }
                cnn.Close();
                funPro();
                LoadChart();
            }
            catch (Exception ex)
            {
                cnn.Close();
                Response.Write("Lỗi: " +ex);
            }
        }

         // Read fund and Profit
        protected void funPro()
        {
            try
            {
                cnnString = ConfigurationManager.ConnectionStrings["DATNconnection"].ToString();
                cnn = new SqlConnection(cnnString);
                cnn.Open();
                string SearchQuery = "SELECT * FROM dbo.Opportunities WHERE Opp_Id = @Opp_Id";
                cmd = new SqlCommand(SearchQuery, cnn);

                cmd.Parameters.Add("@Opp_Id", SqlDbType.Int);
                cmd.Parameters["@Opp_Id"].Value = Convert.ToInt16(ddr_Opp.SelectedValue);
                SqlDataReader readerx = cmd.ExecuteReader();

                while (readerx.Read())
                {
                    lb_Fundx.Text = readerx[6].ToString();
                    lb_Profitx.Text = readerx[7].ToString();
                }
                cnn.Close();
            }catch(Exception ex){

            }
        }
       
        //Load DropDownList
        protected void Drr()
        {
            try
            {
                cnnString = ConfigurationManager.ConnectionStrings["DATNconnection"].ToString();
                cnn = new SqlConnection(cnnString);
                cnn.Open();

                string SearchQuery = "SELECT * FROM dbo.Opportunities WHERE Us_Id = @Us_Id";

                cmd = new SqlCommand(SearchQuery, cnn);

                cmd.Parameters.Add("@Us_Id", SqlDbType.Int);
                cmd.Parameters["@Us_Id"].Value = Convert.ToInt16(Session["Us_Id"]);

                SqlDataReader ddr;
                ddr = cmd.ExecuteReader();

                ddr_Opp.DataSource = ddr;
                ddr_Opp.DataValueField = "Opp_Id";
                ddr_Opp.DataTextField = "Name";
                ddr_Opp.DataBind();
                cnn.Close();
            }
            catch (Exception ex)
            {
                cnn.Close();
                Response.Write("Lôi" + ex);
            }
            finally
            {
                cnn.Close();
            } 
        }


    }
}